package com.peaksport.framework.map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import com.peaksport.framework.exception.PKDevException;

/**
 * 不区分大小写无序Map
 * 实现逻辑：
 *  1. 扩展HashMap,
 *  2. 将key值存储到keyMap<小写字母key,原key>中
 *  3. 实际数据存储到HashMap<原key,Value>中
 *  4. 获取value时,先转换成小写字母key从keyMap获取原key,再到HashMap取Value
 * 
 * @author jinbin_guo  2019年4月17日 上午11:32:24
 *
 * @param <V>
 */
@SuppressWarnings("serial")
public class NoCaseMap<V> extends HashMap<String,V> {

    private Map<String,String> keyMap = new HashMap<>();

    /**
     * 获取key列表
     * @return
     */
    public Collection<String> getKeys() {
    	return keyMap.values();
	}
    

    @Override
    public V get(Object key) {
    	if (key == null) 
   		 	throw PKDevException.throwException("NoCaseMap.get(key),key不能为空");
    	if (!(key instanceof String)) throw PKDevException.throwException("NoCaseMap.get(key),key[%s]类型错误,必须为String类型",key.getClass().getName());
    	String newKey = keyMap.get(((String)key).toLowerCase());
        return super.get(newKey);
    }

    @Override
    public V put(String key,V value) {
    	if (key == null) 
   		 	throw PKDevException.throwException("NoCaseMap.put(key,value),key不能为空");
    	 String newKey = key.toLowerCase();
         keyMap.put(newKey,key);
         return super.put(key,value);
    }

    @Override
    public void putAll(Map<? extends String,? extends V> otherNoCaseMap) {
    	if (otherNoCaseMap == null || otherNoCaseMap.isEmpty()) return;
    	if (!(otherNoCaseMap instanceof NoCaseMap))
    		throw PKDevException.throwException("NoCaseMap.putAll(otherNoCaseMap),otherNoCaseMap[%s]类型错误,必须为NoCaseMap类型", otherNoCaseMap.getClass().getName());
    	for (Map.Entry<? extends String, ? extends V> entry: otherNoCaseMap.entrySet()) {
			String key = entry.getKey();
			V value= entry.getValue();
			put(key,value);
		}
    }
    
    @Override
    public V putIfAbsent(String key, V value) {
    	if (key == null) 
   		 	throw PKDevException.throwException("NoCaseMap.putIfAbsent(key,value),key不能为空");
    	if (value == null) 
    		throw PKDevException.throwException("NoCaseMap.putIfAbsent(key,value),value不能为空");
    	if (get(key) != null) return get(key);
    	return put(key,value);
    }

    @Override
    public void clear() {
    	keyMap.clear();
    	super.clear();
    }
    
    @Override
    public V remove(Object key) {
    	if (key == null) throw PKDevException.throwException("NoCaseMap.remove(key),key不能为空");
    	if (!(key instanceof String)) throw PKDevException.throwException("NoCaseMap.remove(key),key[%s]类型错误,必须为String类型",key.getClass().getName());
    	String newKey = keyMap.get(((String)key).toLowerCase());
    	if (newKey == null) return null;
    	keyMap.remove(key);
    	return super.remove(newKey);
    }

    public void batchReplaceKey(String[] oldKeys, String[] newKeys) {
        if (oldKeys == null || newKeys == null) return;
        if (oldKeys.length != newKeys.length)
            throw PKDevException.throwException("batchReplaceKey(oldKeys,newKeys)数组参数长度不一致.");
        Vector<String[]> vecReplaceKey = new Vector<>();
        for (int i = 0, len = oldKeys.length; i < len; i++) {
            vecReplaceKey.add(new String[] {oldKeys[i],newKeys[i]});
        }
    }

    public void batchReplaceKey(Vector<String[]> vecRepalceKey) {
        if (vecRepalceKey.isEmpty()) return;

        for (int i = 0, len = vecRepalceKey.size(); i < len; i++) {
            String[] replaceKey = vecRepalceKey.get(i);
            replaceKey(replaceKey[0],replaceKey[1]);
        }
    }

    public void replaceKey(String oldKey, String newKey) {
        if (get(newKey) != null)
            throw  PKDevException.throwException("Map已存在key=[%s],不允许替换",newKey);
        V value = remove(oldKey);
        if (value != null) {
            put(newKey,value);
        }
    }
    
}
